package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.BmsMuseumTeamDetail;
import com.macro.mall.dto.BmsMuseumTeamParam;
import com.macro.mall.model.BmsMuseumTeam;
import com.macro.mall.portal.service.BmsMuseumTeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Controller
@Api(tags = "BmsMuseumTeamController")
@Tag(name = "BmsMuseumTeamController", description = "文博项目-报名")
@RequestMapping("/museumTeam")
public class BmsMuseumTeamController {

    @Autowired
    private BmsMuseumTeamService bmsMuseumTeamService;

    @ApiOperation("分页查询文博项目团队-报名")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<BmsMuseumTeam>> getList(@RequestBody BmsMuseumTeamParam bmsMuseumTeamParam) {
        List<BmsMuseumTeam> bmsMuseumTeamList = bmsMuseumTeamService.getList(bmsMuseumTeamParam);
        return CommonResult.success(CommonPage.restPage(bmsMuseumTeamList));
    }

    @ApiOperation("根据id获取文博项目团队-报名信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<BmsMuseumTeamDetail> getItem(@PathVariable Long id) {
        BmsMuseumTeamDetail bmsMuseumTeamDetail = bmsMuseumTeamService.getItem(id);
        return CommonResult.success(bmsMuseumTeamDetail);
    }

    @ApiOperation("查询团队模板下载url")
    @RequestMapping(value = "/getTemplateUrl", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<String> getTemplateUrl() {
        String url = bmsMuseumTeamService.getTemplateUrl();
        return CommonResult.success(url);
    }

    @ApiOperation("根据团队模板文件保存团队预约数据")
    @RequestMapping(value = "/uploadMuseumTeamByFile", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> uploadMuseumTeamByFile (
            @RequestParam("museumGroupId")Long museumGroupId,
            @RequestParam("name")String name,
            @RequestParam("leader")String leader,
            @RequestParam("phone")String phone,
            @RequestParam("applicationTime")String applicationTime,
            @RequestParam("file") MultipartFile file) throws Exception{
        BmsMuseumTeam bmt=new BmsMuseumTeam();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(applicationTime, formatter);
        bmt.setApplicationTime(dateTime);
        bmt.setLeader(leader);
        bmt.setMuseumGroupId(museumGroupId);
        bmt.setName(name);
        bmt.setPhone(phone);
        int result = bmsMuseumTeamService.uploadMuseumTeamByFile(bmt,file);
        return CommonResult.success(result);
    }

}
